{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bit5d211478a4cf4a3d93a2c891f6a9207d",
   "display_name": "Python 3.7.7 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "1,9,25,49\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 16\n",
    "Use a list comprehension to square each odd number(奇数) in a list. The list is input by a sequence of comma-separated numbers. >Suppose the following input is supplied to the program:\n",
    "\n",
    "1,2,3,4,5,6,7,8,9\n",
    "Then, the output should be:\n",
    "\n",
    "1,9,25,49,81\n",
    "'''\n",
    "# for-loop + if \n",
    "lst = input('输入一串数字，用都逗号分割：').split(',')\n",
    "lst_b = []\n",
    "for i in lst:\n",
    "    if int(i)%2 != 0:\n",
    "        lst_b.append(str(int(i)**2))\n",
    "print(','.join(lst_b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "800\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 17\n",
    "Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:\n",
    "\n",
    "D 100\n",
    "W 200\n",
    "D means deposit while W means withdrawal.\n",
    "Suppose the following input is supplied to the program:\n",
    "\n",
    "D 300\n",
    "D 300\n",
    "W 200\n",
    "D 100\n",
    "Then, the output should be:\n",
    "\n",
    "500\n",
    "'''\n",
    "net = 0\n",
    "while True:\n",
    "    x = input(\"存款按D，取款按W：\")\n",
    "    if not x:\n",
    "        break\n",
    "    val = x.split(' ')\n",
    "    if val[0] == 'D':\n",
    "        net += int(val[1])\n",
    "    elif val[0] == 'W':\n",
    "        net -= int(val[1])\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}